#ifndef VIDEODECODER_H
#define VIDEODECODER_H

#include <QObject>
#include <QImage>
#include <QQueue>
#include <QMutex>

extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
}

class VideoDecoder : public QObject
{
    Q_OBJECT
public:
    explicit VideoDecoder(QObject *parent = nullptr);
    ~VideoDecoder();

    bool init();
    void stop();
    void enqueueData(const QByteArray &data);
    bool isInitialized() const { return m_initialized; }

signals:
    void frameDecoded(const QImage &image);

private:
    void decode(const QByteArray &data);
    void cleanup();

    AVCodec *m_codec = nullptr;
    AVCodecContext *m_codecContext = nullptr;
    AVCodecParserContext *m_parser = nullptr;
    AVFrame *m_frame = nullptr;
    SwsContext *m_swsContext = nullptr;

    QQueue<QByteArray> m_dataQueue;
    QMutex m_mutex;
    bool m_initialized = false;
    bool m_running = true;

};

#endif // VIDEODECODER_H
